/**
 * 
 */
package org.xserver.runtime.jetty.command;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.xserver.annotation.PerformanceLog;
import org.xserver.annotation.VisitorLog;

/**
 * @author jkonopka
 * 
 */
public class WriteSettingsCommand extends AbstractSettingsInitializeCommand {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8155454527909406144L;

	/**
	 * @param properties {@link Properties}
	 */
	public WriteSettingsCommand(final Properties properties) {
		super(properties);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xserver.core.command.ICommand#execute()
	 */
	@Override
	@VisitorLog
	@PerformanceLog
	public void execute() throws Exception {
		OutputStream os = null;
		try {
			os = new FileOutputStream(getUserSettingsFile());
			getProperties().storeToXML(os, getComment());
		} finally {
			IOUtils.closeQuietly(os);
		}
	}

	/**
	 * @return comment for settings file header
	 */
	private String getComment() {
		final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(new Date());
	}

}
